package com.design.twords.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.design.twords.MainActivity;
import com.design.twords.R;
import com.design.twords.config.ConfigData;
import com.design.twords.database.UserData;

import org.litepal.LitePal;

import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * Create by Alen
 * Time:  23:27
 */
public class FinishActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView bt_home;
    private Button bt_back;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_finish);

        initView();

        int wordnumb = getIntent().getIntExtra("wordnumb", 0);


        List<UserData> datas =
                LitePal.where("userId = ?", ConfigData.getUserId()).find(UserData.class);
        UserData userData = new UserData();

        if (datas.size() > 0 && !datas.get(0).isAdded()){
            userData.setDayNumb(datas.get(0).getWordNumb()+1);
            userData.setAdded(true);
        }
        userData.setWordNumb(datas.get(0).getWordNumb() + wordnumb);
        userData.updateAll("userId = ?", ConfigData.getUserId());

    }

    private void initView() {
        bt_back = findViewById(R.id.finish_back);
        bt_home = findViewById(R.id.finish_home);

        bt_back.setOnClickListener(this);
        bt_home.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()){
            case R.id.finish_back:
            case R.id.finish_home:
                intent.setClass(FinishActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
